package cn.hloger.spider.demo.dataxDemo.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: LENOVO
 * @Description:
 * @Date: 2022/3/29 17:52
 * @Version: 1.0
 */
@Component
public class SpringApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        setContext(applicationContext);
    }

    public static <T> Map<String, T> getBeans(Class clazz) {
        return applicationContext.getBeansOfType(clazz);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static synchronized void setContext(ApplicationContext applicationContext1) {
        applicationContext = applicationContext1;
    }
}
